#include "Timestamp.h"

#include<time.h>
Timestamp::Timestamp() : microSecondsSinceEpoch_(0) {}
// explicit 和 static 一样不能在定义的时候写在返回类型那里
 Timestamp::Timestamp(int64_t microSecondsSinceEpoch)
 : microSecondsSinceEpoch_(microSecondsSinceEpoch) {}

 // 获取当前的时间
Timestamp Timestamp::now()
{
    // 通过调用time获取当前的时间戳
    return Timestamp(time(NULL));
}
std::string Timestamp::toString() const
{
    char buf[128] = {0};
    // 之前写的http项目没有加时间
    tm* tm_time = localtime(&microSecondsSinceEpoch_);
    snprintf(buf,128,"%4d/%02d/%02d %02d:%02d:%02d",
    tm_time->tm_year + 1900,
    tm_time->tm_mon + 1,
    tm_time->tm_mday,
    tm_time->tm_hour,
    tm_time->tm_min,
    tm_time->tm_sec);
    return buf;
}

// #include<iostream>
// int main()
// {
//     std::cout << Timestamp::now().toString() << std::endl;
//     return 0;
// }